﻿using Ebusiness_Framework.DistributedAuthorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenIddict.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Distributed_AuthenticationService.DistributedAuthorization
{
    public static class OpeniddictClientServiceCollectionExtentions
    {
        public static IServiceCollection AddOpeniddictClientDistributedAuthorization(this IServiceCollection services, IConfiguration configuration)
        {
            OpeniddictClientOptions distributedAuthorizationOptions = configuration.GetSection("OpeniddictClientAuthorization").Get<OpeniddictClientOptions>();
            services.AddOpenIddict()
                            // 配置客户端生成Token的信息
                            .AddClient(options =>
                            {
                                options.UseSystemNetHttp();
                                options.AllowPasswordFlow();
                                options.AllowClientCredentialsFlow();
                                // 禁用令牌存储
                                options.DisableTokenStorage();

                                options.AddRegistration(new OpenIddictClientRegistration
                                {
                                    Issuer = new Uri(distributedAuthorizationOptions.Authority),
                                    ClientId = distributedAuthorizationOptions.ClientId,
                                    ClientSecret = distributedAuthorizationOptions.ClientSecret
                                });

                                options.UseAspNetCore();
                            });
            return services;
        }
    }
}
